package research.ui.editors;

import java.util.Calendar;

import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.Section;
import org.eclipse.ui.forms.widgets.TableWrapData;

import research.domain.Application;
import research.domain.Research;
import research.domain.Result;
import research.entity.EntityType;
import research.model.EntityListContentProvider;
import research.model.ListHierarchy;
import research.persistence.PersistenceManager;
import research.ui.editors.input.EntityEditorInput;

public class ResearchEditor extends FormEntityEditor {
	public static String ID = "research.ui.editors.ResearchEditor";
	
	private Text name;
	private Text idx;
	private ComboViewer application;
	private ComboViewer result;
	private DateTime calendar;
	private Text descr;
	private Text comment;
	
	private Research getEntity() {
		return (Research) entity;
	}

	@Override
	protected boolean checkInputEntityType(EntityEditorInput input) {
		return input.getEntity().getType().equals(EntityType.Research);
	}

	@Override
	protected void fill() {
		this.name.setText(this.getEntity().getName());
		this.idx.setText(this.getEntity().getIdx());
		this.descr.setText(this.getEntity().getDescription());
		this.comment.setText(this.getEntity().getComment());

		Calendar date = Calendar.getInstance();
		date.setTime(this.getEntity().getDate());
		calendar.setYear(date.get(Calendar.YEAR));
		calendar.setMonth(date.get(Calendar.MONTH));
		calendar.setDay(date.get(Calendar.DAY_OF_MONTH));
		
		setComboEntity(application , this.getEntity().getApplication());
		setComboEntity(result , this.getEntity().getResult());
	}

	@Override
	protected void initialize() {
	}

	@Override
	protected boolean readAndValidate() {
		this.getEntity().setName(this.name.getText());
		this.getEntity().setIdx(this.idx.getText());
		this.getEntity().setDescription(this.descr.getText());
		this.getEntity().setComment(this.comment.getText());

		Calendar date = Calendar.getInstance();
		date.set(this.calendar.getYear(), this.calendar.getMonth(), this.calendar.getDay(),
				this.calendar.getHours(), this.calendar.getMinutes(), this.calendar
						.getSeconds());
		this.getEntity().setDate(date.getTime());
		
		Application app = (Application) getComboEntity(application);
		this.getEntity().setApplication(app);
		
		Result res = (Result) getComboEntity(result);
		this.getEntity().setResult(res);
		
		return validateText(name);
	}

	@Override
	protected void renderForm() {
		TableWrapData td;
		Label label;
		Section section;
		Composite composite;
		GridData gd;
		
		/*
		 * Name
		 */
		section = toolkit.createSection(form.getBody(), Section.TITLE_BAR);
		td = new TableWrapData(TableWrapData.FILL);
		section.setLayoutData(td);
		section.setText("Название*");
		composite = toolkit.createComposite(section);
		composite.setLayout(new GridLayout(1, false));
		section.setClient(composite);
		gd = new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL);
		name = toolkit.createText(composite, "",
				SWT.BORDER);
		gd.widthHint = 100;
		name.setLayoutData(gd);
		name.addModifyListener(getNameModifyListener());
		
		/*
		 * Index
		 */
		section = toolkit.createSection(form.getBody(), Section.TITLE_BAR);
		td = new TableWrapData(TableWrapData.FILL);
		section.setLayoutData(td);
		section.setText("Индекс");
		composite = toolkit.createComposite(section);
		composite.setLayout(new GridLayout(1, false));
		section.setClient(composite);
		gd = new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL);
		idx = toolkit.createText(composite, "",
				SWT.BORDER);
		gd.widthHint = 100;
		idx.setLayoutData(gd);
		idx.addModifyListener(getTextModifyListener());
		
		/*
		 * Application
		 */
		section = toolkit.createSection(form.getBody(), Section.TITLE_BAR
				| Section.TWISTIE | Section.EXPANDED);
		td = new TableWrapData(TableWrapData.FILL);
		section.setLayoutData(td);
		section.setText("Документация");
		composite = toolkit.createComposite(section);
		composite.setLayout(new GridLayout(2, false));
		section.setClient(composite);
		gd = new GridData();
		label = toolkit.createLabel(composite, "Заявка на исследование:");
		label.setLayoutData(gd);
		gd = new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL);
		application = new ComboViewer(composite, SWT.DROP_DOWN | SWT.READ_ONLY);
		application.setContentProvider(new EntityListContentProvider(new ListHierarchy(EntityType.Application), true));
		application.setLabelProvider(new LabelProvider());
		application.setInput(PersistenceManager.getInstance());
		gd.widthHint = 100;
		application.getCombo().setLayoutData(gd);
		application.getCombo().addModifyListener(getTextModifyListener());
		
		/*
		 * Result
		 */
		gd = new GridData();
		label = toolkit.createLabel(composite, "Отчет о результатах:");
		label.setLayoutData(gd);
		gd = new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL);
		result = new ComboViewer(composite, SWT.DROP_DOWN | SWT.READ_ONLY);
		result.setContentProvider(new EntityListContentProvider(new ListHierarchy(EntityType.Result), true));
		result.setLabelProvider(new LabelProvider());
		result.setInput(PersistenceManager.getInstance());
		gd.widthHint = 100;
		result.getCombo().setLayoutData(gd);
		result.getCombo().addModifyListener(getTextModifyListener());

		/*
		 * Date Time
		 */
		section = toolkit.createSection(form.getBody(), Section.TITLE_BAR);
		td = new TableWrapData(TableWrapData.FILL);
		section.setLayoutData(td);
		section.setText("Дата проведения*");
		composite = toolkit.createComposite(section);
		composite.setLayout(new GridLayout(1, false));
		section.setClient(composite);
		gd = new GridData(GridData.FILL_BOTH);
		calendar = new DateTime(composite, SWT.CALENDAR);
		calendar.setLayoutData(gd);
		calendar.addSelectionListener(getDateSelectionListener());

		/*
		 * Description
		 */
		section = toolkit.createSection(form.getBody(), Section.TITLE_BAR
				| Section.EXPANDED | Section.TITLE_BAR | Section.TWISTIE);
		td = new TableWrapData(TableWrapData.FILL_GRAB);
		td.maxHeight = 300;
		section.setLayoutData(td);
		section.setText("Описание");
		composite = toolkit.createComposite(section);
		composite.setLayout(new GridLayout(1, false));
		section.setClient(composite);
		gd = new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL);
		descr = toolkit.createText(composite, "",
				SWT.BORDER | SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
		gd.widthHint = 200;
		gd.heightHint = 60;
		descr.setLayoutData(gd);
		descr.addModifyListener(getTextModifyListener());
		
		/*
		 * Comment
		 */
		section = toolkit.createSection(form.getBody(), Section.TITLE_BAR
				| Section.EXPANDED | Section.TITLE_BAR | Section.TWISTIE);
		td = new TableWrapData(TableWrapData.FILL_GRAB);
		td.maxHeight = 300;
		section.setLayoutData(td);
		section.setText("Комментарий");
		composite = toolkit.createComposite(section);
		composite.setLayout(new GridLayout(1, false));
		section.setClient(composite);
		gd = new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL);
		comment = toolkit.createText(composite, "",
				SWT.BORDER | SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
		gd.widthHint = 200;
		gd.heightHint = 60;
		comment.setLayoutData(gd);
		comment.addModifyListener(getTextModifyListener());

	}

}
